package com.example.service.impl;

import com.example.mapper.ApartmentMapper;
import com.example.mapper.BrowseMapper;
import com.example.pojo.Apartment;
import com.example.pojo.Browse;
import com.example.service.BrowseService;
import com.example.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class BrowseServiceImpl implements BrowseService {
    @Autowired
    private BrowseMapper browseMapper;

    @Autowired
    private ApartmentMapper apartmentMapper;

    @Override
    public void addBrowse(Browse browse, HttpServletRequest request) {
        Integer id = browse.getIdA();
        Apartment apartment=apartmentMapper.getApartmentId(id);
        browse.setName(apartment.getName());
        browse.setArea(apartment.getArea());
        browse.setRoomType(apartment.getRoomType());
        browse.setPrice(apartment.getPrice());
        browse.setDate(LocalDateTime.now());
        browse.setImage(apartment.getImage());
        browse.setIdU(JwtUtils.parseJWT(request.getHeader("token")).get("id", Integer.class));
        browseMapper.addBrowse(browse);
    }

    @Override
    public void delBrowse(Integer id, HttpServletRequest request) {
        Integer idU = JwtUtils.parseJWT(request.getHeader("token")).get("id", Integer.class);
        browseMapper.delBrowse(id,idU);
    }

    @Override
    public List<Browse> getBrowse(LocalDateTime date,HttpServletRequest request) {
        List<Browse> list=browseMapper.getBrowse(JwtUtils.parseJWT(request.getHeader("token")).get("id",Integer.class),date);
        return list;
    }
}
